| Conditions | 12 |
| Total Lines | 68 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like reactrouter.js ➔ instrumentReactRouter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import { browserTracingIntegration, startBrowserTracingPageLoadSpan, startBrowserTracingNavigationSpan, WINDOW } from '@sentry/browser'; |
||
| 74 | |||
| 75 | function instrumentReactRouter( |
||
| 76 | client, |
||
| 77 | instrumentPageLoad, |
||
| 78 | instrumentNavigation, |
||
| 79 | history, |
||
| 80 | instrumentationName, |
||
| 81 | allRoutes = [], |
||
| 82 | matchPath, |
||
| 83 | ) { |
||
| 84 | function getInitPathName() { |
||
| 85 | if (history.location) { |
||
| 86 | return history.location.pathname; |
||
| 87 | } |
||
| 88 | |||
| 89 | if (WINDOW.location) { |
||
| 90 | return WINDOW.location.pathname; |
||
| 91 | } |
||
| 92 | |||
| 93 | return undefined; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Normalizes a transaction name. Returns the new name as well as the |
||
| 98 | * source of the transaction. |
||
| 99 | * |
||
| 100 | * @param pathname The initial pathname we normalize |
||
| 101 | */ |
||
| 102 | function normalizeTransactionName(pathname) { |
||
| 103 | if (allRoutes.length === 0 || !matchPath) { |
||
| 104 | return [pathname, 'url']; |
||
| 105 | } |
||
| 106 | |||
| 107 | const branches = matchRoutes(allRoutes, pathname, matchPath); |
||
| 108 | for (const branch of branches) { |
||
| 109 | if (branch.match.isExact) { |
||
| 110 | return [branch.match.path, 'route']; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | return [pathname, 'url']; |
||
| 115 | } |
||
| 116 | |||
| 117 | if (instrumentPageLoad) { |
||
| 118 | const initPathName = getInitPathName(); |
||
| 119 | if (initPathName) { |
||
| 120 | const [name, source] = normalizeTransactionName(initPathName); |
||
| 121 | startBrowserTracingPageLoadSpan(client, { |
||
| 122 | name, |
||
| 123 | attributes: { |
||
| 124 | [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload', |
||
| 125 | [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: `auto.pageload.react.${instrumentationName}`, |
||
| 126 | [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source, |
||
| 127 | }, |
||
| 128 | }); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | if (instrumentNavigation && history.listen) { |
||
| 133 | history.listen((location, action) => { |
||
| 134 | if (action && (action === 'PUSH' || action === 'POP')) { |
||
| 135 | const [name, source] = normalizeTransactionName(location.pathname); |
||
| 136 | startBrowserTracingNavigationSpan(client, { |
||
| 137 | name, |
||
| 138 | attributes: { |
||
| 139 | [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation', |
||
| 140 | [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: `auto.navigation.react.${instrumentationName}`, |
||
| 141 | [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source, |
||
| 142 | }, |
||
| 233 |